package com.baiduspring.vo;

import com.baiduspring.dao.entity.User;
import com.baiduspring.utils.Validator;
import com.baiduspring.utils.date.DateUtils;
import com.baiduspring.utils.security.SystemSecurityEncrypt;
import com.google.common.base.Preconditions;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Data
@Slf4j
public class UserVO implements BaseVO<UserVO, User> {
    private Long id;

    private String userName;

    private String password;

    private String phoneNumber;

    private String nickname;

    private String email;

    private Integer qq;

    private String motto;

    private String photo;

    private Integer sex;

    private String personalWebsite;

    private String personalProfile;

    private String ip;

    private Date createDate;

    private Long account;

    private Long homepageViews;

    private UserDetailsVO userDetails;

    @Override
    public UserVO validate() {
        if (Objects.nonNull(this.phoneNumber)) {
            Preconditions.checkArgument(Validator.isMobile(this.phoneNumber), "电话号码格式不正确");
        }
        if (!StringUtils.isEmpty(this.email)) {
            Preconditions.checkArgument(Validator.isEmail(this.email), "Email格式不正确");
        }
        if (!StringUtils.isEmpty(this.password)) {
            this.password = SystemSecurityEncrypt.encrypt(this.password);
        }
        return this;
    }

    public String showName() {
        return !StringUtils.isEmpty(this.getNickname()) ? this.getNickname() :
                !StringUtils.isEmpty(this.getUserName()) ? this.getUserName() : !StringUtils.isEmpty(this.getPhoneNumber()) ? this.getPhoneNumber() : "";
    }

    public String createDate() {
        if (Objects.nonNull(this.createDate)) {
            return DateUtils.formatDate2String(this.createDate, "yyyy年MM月dd日");
        }
        return null;
    }

    public String homepageViews() {
        long num = 999L;
        if (this.homepageViews > num) {
            BigDecimal homepageViews = new BigDecimal(String.valueOf(this.homepageViews));
            BigDecimal div = new BigDecimal(String.valueOf(1000L));
            BigDecimal result = homepageViews.divide(div, 1, BigDecimal.ROUND_HALF_UP);
            return result.toString() + "K";
        }

        return String.valueOf(this.homepageViews);
    }

}
